//
//  MovieDetailCollectionViewController.m
//  UI-13-DouBan--
//
//  Created by lanou3g on 15/9/4.
//  Copyright (c) 2015年 自由科技. All rights reserved.
//
#define kDetailURL @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php"
#import "MovieDetailCollectionViewController.h"
#import "MovieTableViewController.h"
#import "MovieDetailViewController.h"

#import "MovieDetailCollectionViewCell.h"

#import "Movie.h"
#import "UIImageView+WebCache.h"

@interface MovieDetailCollectionViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) MovieDetailCollectionViewCell *detailCell;

@end

@implementation MovieDetailCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    
    //返回九宫格
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_list"] style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    
#warning 隐藏back
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:UIBarMetricsDefault];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_nav"] forBarMetrics:UIBarMetricsDefault];
    
    
    UICollectionViewFlowLayout *floLayout = [[UICollectionViewFlowLayout alloc] init];
    
//    UICollectionViewFlowLayout * floLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    
    floLayout.itemSize = CGSizeMake(100, 140);
    floLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每一列的最小间距
    floLayout.minimumInteritemSpacing = 20;
    //设置每一行的最小间距
    floLayout.minimumLineSpacing = 40;
    //设置header
    floLayout.headerReferenceSize = CGSizeMake(50, 30);
    //设置footer
    floLayout.footerReferenceSize = CGSizeMake(30, 50);
    //设置item的内边距大小(上左下右)
    floLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

    
    UICollectionView *cView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.frame.size.height-64) collectionViewLayout:floLayout];
    cView.backgroundColor = [UIColor whiteColor];
    
    cView.delegate = self;
    cView.dataSource = self;
    
    
    [cView registerClass:[MovieDetailCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    NSLog(@"%lu",_dataArray.count);
    [self.view addSubview:cView];
}

- (void)back:(UIBarButtonItem *)sender{
#warning pop
    [self.navigationController popToRootViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MovieDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    Movie *model = _dataArray[indexPath.item];
    cell.movieTitleLabel.text = model.movieName;
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"picholder"]];
    
    return cell;
}

//处理collectionView 每个item 的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld,   %ld",indexPath.section, indexPath.row);
    
    
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc] init];
    
    // http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=216609
    Movie *movie = _dataArray[indexPath.row];
    
    //    NSURL *URL = [NSURL URLWithString:kDetailURL];
    //    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
    NSString *body = [NSString stringWithFormat:@"%@?movieId=%@",kDetailURL, movie.movieId];
    
    NSURL *url = [NSURL URLWithString:body];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *dic111 = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    NSDictionary *resultDic = [dic111 objectForKey:@"result"];
    
    Movie *mmm = [[Movie alloc] init];
    [mmm setValuesForKeysWithDictionary:resultDic];
    
    //    movieDetailVC.movieDetailView.ratingLabel.text = mmm.rating;
    movieDetailVC.movieModel = mmm;
    
    [self.navigationController pushViewController:movieDetailVC animated:YES];
    
}

#pragma mark <UICollectionViewDelegate>


@end
